#include "mosaicdialog.h"
#include "ui_mosaicdialog.h"

#include <QPainter>

MosaicDialog::MosaicDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MosaicDialog)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setWindowTitle(tr("Mosaic"));
    setModal(true);
    setFixedSize(343, 128);

    connect(ui->sizeSlider, SIGNAL(valueChanged(int)), ui->sizeSpinBox, SLOT(setValue(int)));
    connect(ui->sizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(emitSizeChanged(int)));
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
}

MosaicDialog::~MosaicDialog()
{
    delete ui;
}

void MosaicDialog::setSize(int size)
{
    ui->sizeSpinBox->setValue(size);
}

void MosaicDialog::setSizeMaximum(int max)
{
    ui->sizeSlider->setMaximum(max);
    ui->sizeSpinBox->setMaximum(max);
}

void MosaicDialog::emitSizeChanged(int size)
{
    ui->sizeSlider->setValue(size);
    emit sizeChanged(size);
}

void MosaicDialog::applyButtonClicked()
{
    QString temp = QString::number(ui->sizeSlider->value());
    QString str = tr("Do mosaic. Size : ")
            + temp + tr("x") + temp + tr(".");
    emit applyOperator(str);
    close();
}

void MosaicDialog::cancelButtonClicked()
{
    close();
}

void MosaicDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void MosaicDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void MosaicDialog::showEvent(QShowEvent *)
{
    ui->sizeSlider->setValue(2);
}
